package com.hotel.adminuser.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hotel.adminuser.service.AdminUserService;
import com.jt.common.utils.CookieUtils;

@RestController
@RequestMapping("/admin/manage")
public class AdminUserController {
	
	@Autowired
	private AdminUserService adminUserService;
		//后台登录校验
		@RequestMapping("look")
		public Map<Object,Object> doLook(String username,String password,HttpServletRequest
				req,HttpServletResponse res){
			//在业务层中实现登录逻辑,存储redis过程
			//返回给控制层一个生成的redis存储ticket值
			//登录成功,ticket是非空,登录失败返回空null/""
			Map<Object ,Object> map = new HashMap<Object,Object>();
			try{
				
				String ticket=adminUserService.doLook(username,password);
				if(StringUtils.isNotEmpty(ticket)){
					//ticket非空,说明登录成功
					//cookie携带这个ticket返回浏览Cookie的工具类
					CookieUtils.setCookie(req, res, 
							"EM_TICKET", ticket);
					map.put("success", true);
					map.put("message", "登陆成功");
				}else{
					map.put("success", false);
					map.put("message", "登陆失败");
				}
			}catch(Exception e){
				e.printStackTrace();
				map.put("success", false);
			}
			return map;
		}
		
}
		
